package com.cater.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.Type;
import com.cater.sys.service.impl.TypeServiceImpl;

/**
 * @author 作者: yy
 * @date 创建时间：2017年1月11日 上午10:04:48
 * @version 1.0
 */
@Controller
@RequestMapping("/type")
public class TypeController {
	@Autowired
	private TypeServiceImpl typeService;

	/**
	 * 数据列表视图
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list() {
		return "sys/type/list";
	}

	/**
	 * 获取 数据分页
	 * 
	 * @param typeQuery
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/listData")
	@SystemControllerLog(description = "查看类别")
	public PageResult<Type> getAll(CommonQuery typeQuery, HttpServletRequest request) {
		PageResult<Type> result = typeService.findPageResult(typeQuery);
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getTypeBypid")
	@SystemControllerLog(description = "根据类别ID查询子集")
	public JSONObject getdepBypid(Integer nodeid) {
		JSONObject json = new JSONObject();
		if (nodeid == null) {
			nodeid = 0;
		}
		/*TreeUtil treeUtil = new TreeUtil();
		TypeTreeObject[] rows = treeUtil.getTypeTreeObjects(typeService.getTypeBypid(nodeid));
		json.put("rows", rows);*/
		return json;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("type:add")
	public String addView(Model model,Integer pid, Integer level) {
		List<Type> list = typeService.getParent();
		model.addAttribute("type", list);
		model.addAttribute("pid", pid);
		model.addAttribute("level", level);
		return "sys/type/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("type:add")
	@ResponseBody
	@SystemControllerLog(description = "添加类别")
	public JSONObject add(Type type) {
		JSONObject json = new JSONObject();
		int id = typeService.save(type);
		if (id != 0) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("type:update")
	public String editView(String id, Model model) {
		Type type = typeService.getById(id);
		model.addAttribute("type", type);
		List<Type> list = typeService.getParent();
		model.addAttribute("typeparent", list);
		return "sys/type/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("type:update")
	@ResponseBody
	@SystemControllerLog(description = "修改类别")
	public JSONObject update(Type type) {
		JSONObject json = new JSONObject();
		try {
			typeService.update(type);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@SystemControllerLog(description = "删除类别")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		List<Type> list = typeService.getChild(Integer.valueOf(id));
		if (list.size() != 0) {
			json.put("code", 999);
			json.put("message", "该类别存在子集，无法删除！");
		} else {
			try {
				typeService.delete(id);
				json.put("code", 200);
				json.put("message", "删除成功");
			} catch (Exception e) {
				e.printStackTrace();
				json.put("code", 999);
				json.put("message", "删除失败");
			}
		}
		return json;
	}

}
